package demo;

import cn.hutool.core.util.RandomUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.DelayQueue;
import java.util.concurrent.TimeUnit;

/**
 * 描述
 *
 * @author 韦鹏
 * @date 2022/11/13 15:59
 */
@Slf4j
public class DelayQueueDemo {

    public static void main(String[] args) throws InterruptedException {
        DelayQueue<OrderDelay> queue = new DelayQueue<>();


        // 添加任务
        queue.put(new OrderDelay(RandomUtil.randomString(5), TimeUnit.NANOSECONDS.convert(3, TimeUnit.SECONDS)));
        queue.put(new OrderDelay(RandomUtil.randomString(5), TimeUnit.NANOSECONDS.convert(1, TimeUnit.SECONDS)));
        queue.put(new OrderDelay(RandomUtil.randomString(5), TimeUnit.NANOSECONDS.convert(6, TimeUnit.SECONDS)));
        queue.put(new OrderDelay(RandomUtil.randomString(5), TimeUnit.NANOSECONDS.convert(10, TimeUnit.SECONDS)));

        while (true) {
            //延迟三秒取出,take阻塞拿 poll非阻塞，会返回null,peek
            queue.take();
            log.debug("take");
        }


    }
}
